package cn.com.pxto.dao;

import java.util.HashMap;
import java.util.List;

import cn.com.pxto.commons.pagesupport.PageInfo;
import cn.com.pxto.dao.pagesupport.hibernate.HibernatePageDaoSupport;
import cn.com.pxto.model.LessonType;

public class LessonTypeDAO extends HibernatePageDaoSupport {
	public LessonType getLessonType(Integer ltid){
		return (LessonType) getTemplate().get(LessonType.class, ltid);
	}
	/*
	 * return null if can't find
	 */
	@SuppressWarnings("unchecked")
	public HashMap<String ,String> getLessonTypeForTag(){
		HashMap<String ,String> lessonTypeMap=new HashMap();
		StringBuffer HQL=new StringBuffer();
		HQL.append("select lt.ltid,lt.name from LessonType as lt  ");
		HQL.append(" order by lt.ltid");
		List list=getTemplate().find(HQL.toString());
		if(list!=null && list.size()>0){
			for (int i = 0; i < list.size(); i++) {
				Object[] obj=(Object[]) list.get(i);
				lessonTypeMap.put(((Integer)obj[0]).toString(), (String)obj[1]);
			}			
			return lessonTypeMap;
		}else{
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public String getLessonTypeByString(String lessonTypeStr){
		StringBuffer lessonTypeNameStr=new StringBuffer();
		StringBuffer HQL=new StringBuffer();
		HQL.append("select lt.name from LessonType as lt ");
		HQL.append(" where lt.ltid in ( ");
		HQL.append(lessonTypeStr);
		HQL.append(" )");
		List list=getTemplate().find(HQL.toString());
		if(list!=null && list.size()>0){
			for (int i = 0; i < list.size(); i++) {
				String lessonTypeName=(String) list.get(i);
				lessonTypeNameStr.append(lessonTypeName);
				lessonTypeNameStr.append(",");
			}			
			return lessonTypeNameStr.substring(0, lessonTypeNameStr.length()-1);
		}else{
			return "";
		}
	}
	public void saveLessonType(LessonType lessonType){
		getTemplate().save(lessonType);
	}
	
	public void deleteLessonType(Integer ltid){
		String HQL="from LessonType as lt where lt.ltid="+ltid;
		getTemplate().delete(HQL);
	}
	
	public void updateLessonType(LessonType lessonType){
		getTemplate().saveOrUpdate(lessonType);
	}
	
	/*
	 * return null if can't find
	 */
	@SuppressWarnings("unchecked")
	public LessonType getLessonType(String name){
		String HQL="select lt from LessonType as lt where lt.name='"+name+"'";
		List list=getTemplate().find(HQL);
		if(list!=null && list.size()>0){
			return (LessonType)list.get(0);
		}else{
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<LessonType> listLessonType(PageInfo pageInfo){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select lt from LessonType as lt  ");
		HQL.append(" order by lt.ltid");
		return getTemplate().pagedFind(HQL.toString(), pageInfo);
	}
	
	@SuppressWarnings("unchecked")
	public List<LessonType> listLessonType(){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select lt from LessonType as lt  ");
		HQL.append(" order by lt.ltid");
		return getTemplate().find(HQL.toString());
	}
}
